#include "proj_config.h"
#include "system_ln88xx.h"
#include "hal/hal_interrupt.h"
#include "ln_kv_api.h"
#include "flash_partition_table.h"

#include "utils/debug/log.h"
#include "utils/runtime/runtime.h"
#include "utils/reboot_trace/reboot_trace.h"
#include "utils/debug/CmBacktrace/cm_backtrace.h"


int main (int argc, char* argv[])
{
    //0. check reboot cause
    ln_chip_get_reboot_cause();

    //1.sys clock,interrupt
    SetSysClock();
    set_interrupt_priority();
    switch_global_interrupt(true);
    ln_runtime_measure_init();

    //2.init log
    log_init();

    cm_backtrace_init("mcu_os_none_demo", "HW_V1.0", "SW_V1.0");
    LOG(LOG_LVL_INFO, "------  mcu_os_none_demo  ------\r\n");

    if (KV_ERR_NONE != ln_kv_port_init(KV_SPACE_OFFSET, (KV_SPACE_OFFSET + KV_SPACE_SIZE))) {
        LOG(LOG_LVL_ERROR, "KV init filed!\r\n");
    }

    uint32_t count = 0;
    char value_set[100] = { 0 };
    char value_read[100] = { 0 };
    size_t value_len = 0;

    while (1)
    {
        kv_err_t err;
        ln_block_delayms(1000);
        LOG(LOG_LVL_ERROR, "os none run count = %d\r\n", count++);

        memset(value_set, 0, 100);
        sprintf((char *)value_set, "abcd-#%d", count);

        err = ln_kv_set("K1", value_set, strlen(value_set));
        if (err != KV_ERR_NONE) {
            LOG(LOG_LVL_ERROR, "ERROR: kv set!!!\n");
            while(1);
        }

        memset(value_read, 0, 100);
        err = ln_kv_get("K1", (void *)value_read, 100, &value_len);
        if (err != KV_ERR_NONE) {
            LOG(LOG_LVL_ERROR, "ERROR: kv get!!!\n");
            while(1);
        }
        LOG(LOG_LVL_INFO, "value read: %s\n", value_read);
    }
}

// ----------------------------------------------------------------------------
